﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.IO;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Text;
using System.Threading.Tasks;
using WPF_PathSearchUtil.entity;
using WPF_PathSearchUtil.utils;

namespace WPF_PathSearchUtil.service
{
    internal class LoadFileList
    {
        public static Resp WithdrawPath(ref ObservableCollection<PathInfo> fileList,string filePath,bool isChecked)
        {
            PathInfo pathInfos;
            if (filePath.LastIndexOf('\\') != filePath.Length - 1)
            {
                filePath = filePath + "\\";
            }
            DirectoryInfo directory = new DirectoryInfo(filePath);
            if (directory.Exists)
            {
                FileInfo[] fileInfos;
                DirectoryInfo[] directoryInfos;
                fileInfos = directory.GetFiles("*", SearchOption.TopDirectoryOnly);
                directoryInfos = directory.GetDirectories("*", SearchOption.TopDirectoryOnly);
                foreach (FileInfo fileInfo in fileInfos)
                {
                    pathInfos = new PathInfo();
                    pathInfos.FileName = fileInfo.Name;
                    int index = fileInfo.FullName.LastIndexOf("\\");
                    pathInfos.FilePath = fileInfo.FullName.Substring(0, index + 1);
                    pathInfos.FileSize = Math.Round(Double.Parse(fileInfo.Length.ToString()) / 1024, 2);
                    pathInfos.FileType = fileInfo.Extension.Substring(1, fileInfo.Extension.Length - 1);
                    pathInfos.FileDate = fileInfo.CreationTime;
                    fileList.Add(pathInfos);
                }
                foreach (DirectoryInfo directoryInfo in directoryInfos)
                {
                    pathInfos = new PathInfo();
                    pathInfos.FileName = directoryInfo.Name;
                    int index = directoryInfo.FullName.LastIndexOf("\\");
                    pathInfos.FilePath = directoryInfo.FullName.Substring(0, index + 1);
                    pathInfos.FileSize = Math.Round(Double.Parse(GetDirectorySize(directoryInfo.FullName).ToString()) / 1024, 2);
                    pathInfos.FileType = "文件夹";
                    pathInfos.FileDate = directoryInfo.CreationTime;
                    fileList.Add(pathInfos);
                }
                if (isChecked)
                {

                    foreach (DirectoryInfo dirInfo in directoryInfos)
                    {
                        try
                        {
                            WithdrawPath(ref fileList, dirInfo.FullName, isChecked);
                        }
                        catch
                        {
                            
                        }
                    }
                }
            }
            else
            {
                return Resp.Fail("目录不存在。");
            }
            return Resp.Success();
        }

        /// <summary>
        ///     获取文件夹的大小
        /// </summary>
        /// <param name="filePath"></param>
        /// <returns></returns>
        public static double GetDirectorySize(String filePath)
        {
            double size = 0;
            try
            {
                DirectoryInfo directory = new DirectoryInfo(filePath);
                FileInfo[] fileInfos = directory.GetFiles("*", SearchOption.TopDirectoryOnly);
                foreach (FileInfo fileInfo in fileInfos)
                {
                    size += fileInfo.Length;
                }
            }
            catch { }
            return size;
        }
    }
}
